﻿// Copyright @ shch8.com All Rights Reserved At 2008-2-26
//开发：商创技术（www.shch8.com）望月狼
//修改：2009-5-22 10:33
/*
·注释类。
参数：txtNote(背景颜色,注释字颜色,透明度，缓动速度,延时,边线透明度，线条色);
例：
import com._public._module.txtNote
var exec=new txtNote(0xffffcc,0x000000,1,3,0,1,0x999999);
addChild(exec);
txtNote.showBox("上传图片文件",100,true);// @ 显视注释框 txtNote.showBox(显视文本,宽度,是否自动调整位置) @当宽度为零时不自动换行
txtNote.hideBox();// @ 隐藏注释框
*/
package com._public._ui{
	import flash.display.*;
	import flash.events.*;
	import flash.geom.ColorTransform;
	import flash.geom.Rectangle;
	import flash.geom.Transform;
	import flash.text.*;
	import flash.utils.Timer;
	public class txtNote extends Sprite {
		private var MC:MovieClip;
		private var bgColor:uint;
		private var txtColor:uint;
		private var txtBox:TextField;
		private var grid:Rectangle;
		private var bgRect:Shape;
		private var scaleFactor:Number = 1.01;
		private var end:Number;
		private var autoSet:Boolean;
		private var addY:Number;
		private var addX:Number;
		private var oldWid:Number;
		private var oldHei:Number;
		private var speed:Number;
		private var showTime:uint;
		private var useTimer:Timer;
		private var clipBox:Sprite = new Sprite;
		private var showOr:Boolean = false;//当前是否显示
		public static  var setXY:Function;
		/**
		 * @ 显视注释框
		 * 
		 * txtNote.showBox(显视文本,宽度,是否自动调整位置,是否跟随,显示延时) 
		 * 当宽度为零时不自动换行
		 * @param txt		<String> 显视文本
		 * @param wid		<Number> 宽度
		 * @param auto		<Boolean (default=true)> 是否自动调整位置
		 * @param follow	<Boolean (default=true)> 是否跟随
		 * @param _showTime	<Number (default=-1.0101)> 显示延时
		 * */
		public static  var showBox:Function;
		/* @ 隐藏注释框*/
		public static  var hideBox:Function;
		/**注释类
		 * 参数：txtNote(背景颜色,注释字颜色,透明度，缓动速度,延时,边线透明度，线条色);
		 * @param _bgColor 	<default=0x000000> 背景颜色
		 * */
		public function txtNote(_bgColor:uint=0x000000,_txtColor:uint=0x999999,_alpha:Number=0.5,_speed:Number=5,_showTime:uint=0,_lineAlpha:Number=0,_lineColor:uint=0x999999) {
			if(_speed<1){
				_speed=1
				}
			showTime=_showTime;
			speed=_speed;
			bgColor=_bgColor;
			txtColor=_txtColor;
			bgRect=new Shape();
			bgRect.graphics.beginFill(bgColor);
			if (_lineAlpha!=0) {
				bgRect.graphics.lineStyle(1,_lineColor,_lineAlpha,true);
			}
			bgRect.graphics.drawRoundRect(0,0,80,20,8,8);//画矩形背景
			grid= new Rectangle(5, 5, 10,10);
			bgRect.scale9Grid = grid ;
			bgRect.alpha=_alpha;
			clipBox.addChild(bgRect);

			txtBox= new TextField();
			txtBox.textColor=txtColor;
			txtBox.x=3;
			txtBox.text="--";
			//txtBox.autoSize=TextFieldAutoSize.LEFT;
			//txtBox.border=true;
			txtBox.selectable = false;
			clipBox.addChild(txtBox);
			showBox=showBox_fun;
			hideBox=hideBox_fun;
			setXY=setXY_fun;
		}
		private function showBox_fun(txt:String, wid:Number=50, auto:Boolean = true, follow:Boolean = true, _showTime:Number = -1.0101):void {
			if (!showOr) {//未显示
				setXY_fun(stage.mouseX-20, stage.mouseY-20);
				addChild(clipBox);
				showOr = true;
				}else {
					useTimerFun("")
					return;
					}
			if (_showTime != -1.0101) {
				showTime = _showTime;
				}
			if (showTime!=0) {
				useTimer=new Timer(showTime);//延时设计时间,为调整文本块
				useTimer.addEventListener(TimerEvent.TIMER, useTimerFun);
				useTimer.start();
			} else {
				useTimerFun("");
			}
			function useTimerFun(e:*):void {
				if (useTimer!=null) {
					useTimer.stop();
					useTimer.removeEventListener(TimerEvent.TIMER, seting);
				}
				txtBox.wordWrap=false;
				txtBox.text=txt;
				txtBox.autoSize=TextFieldAutoSize.LEFT;
				autoSet = auto;
				
				if (wid==0) {//宽为0,则不换行单行注释
					txtBox.autoSize=TextFieldAutoSize.LEFT;
					seting();
				} else {
					txtBox.autoSize=TextFieldAutoSize.NONE;
					txtBox.wordWrap=true;
					var myTimer:Timer=new Timer(10);//延时设计时间,为调整文本块
					myTimer.addEventListener(TimerEvent.TIMER, timerHandler);
					myTimer.start();
				}
				function timerHandler(e:Event):void {
					myTimer.removeEventListener(TimerEvent.TIMER, timerHandler);
					if (txtBox.width>wid) {//只有一行
						txtBox.width=wid;
						txtBox.autoSize=TextFieldAutoSize.CENTER;
					}
					seting(follow);
				}
			}
		}
		private function seting(follow:Boolean=true):void {
			bgRect.width=txtBox.textWidth+10;
			bgRect.height=txtBox.textHeight+5;
			if (! follow) {
				return;
			}
			stage.addEventListener(Event.ENTER_FRAME,enter_Frame);
			if (! autoSet) {
				if (stage.mouseX<stage.stageWidth/2) {
					addX=10;
				}
				if (stage.mouseX>=stage.stageWidth/2) {
					addX=0-bgRect.width-10;
				}
				if (stage.mouseY<stage.stageHeight/2) {
					addY=15;
				}
				if (stage.mouseY>=stage.stageHeight/2) {
					addY=0-bgRect.height;
				}
				this.x=stage.mouseX+addX;
				this.y=stage.mouseY+addY;
			}
		}
		private function setXY_fun(setX:Number=0,setY:Number=0):void {
			this.x=setX;
			this.y=setY;
		}
		private function hideBox_fun():void {
			if (!showOr) return;
			if (useTimer!=null) {
				useTimer.stop();
			}
			stage.removeEventListener(Event.ENTER_FRAME,enter_Frame);
			removeChild(clipBox)
			showOr = false;
		}
		private function enter_Frame(e:Event):void {
			if (autoSet) {
				if (stage.mouseX<stage.stageWidth/2) {
					this.x+=(stage.mouseX+15-this.x)/speed;
				}
				if (stage.mouseX>=stage.stageWidth/2) {
					this.x+=(stage.mouseX-15-this.x-bgRect.width)/speed;
				}
				if (stage.mouseY<stage.stageHeight/2) {
					this.y+=(stage.mouseY+15-this.y)/speed;
				}
				if (stage.mouseY>=stage.stageHeight/2) {
					this.y+=(stage.mouseY-this.y-bgRect.height)/speed;
				}
			} else {
				this.x=stage.mouseX+addX;
				this.y=stage.mouseY+addY;
			}
		}
	}
}